package com.supervision.manage.service

import com.fly.base.misc.upload.FileSystemClient
import com.fly.base.misc.upload.UploadObject
import org.apache.commons.codec.digest.DigestUtils
import org.apache.commons.io.FilenameUtils
import org.springframework.stereotype.Service
import org.springframework.web.multipart.MultipartFile

@Service
class FileService {

    fun fileUpload(file: MultipartFile): UploadObject? {
        val fileOriginalFilename = file.originalFilename // 文件原始名字
        val mimeType = file.contentType // 文件mime

        val ext = FilenameUtils.getExtension(fileOriginalFilename) // 文件扩展名

        val fileHash = DigestUtils.md5Hex(file.inputStream)
        val ossKey = FileSystemClient.getFileKey("$fileHash.$ext")

        return FileSystemClient.getClient().upload(fileOriginalFilename, file.inputStream, ossKey, mimeType)
    }
}